package com.example.companybusiness;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.companybusiness.publicoperation.AttentionFragment;
import com.example.companybusiness.operationt.fragment.HomeFragment;
import com.example.companybusiness.operationl.MediaReportsFragment;
import com.example.companybusiness.operationf.MineFragment;
import com.example.companybusiness.operationc.StarbrandFragment;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Fragment> fragmentList;
    BottomNavigationView navigation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragment();
    }

    private void initFragment() {
        ViewPager pager=findViewById(R.id.bottom_view_pager);
        navigation=findViewById(R.id.bottom_navigation_menu);
        //创建 viewpager+bottomNavigation适配器
        BottomPagerAdapter adapter = new BottomPagerAdapter(getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        //创建 list并添加 fragment
        fragmentList=new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new StarbrandFragment());
        fragmentList.add(new MediaReportsFragment());
        fragmentList.add(new AttentionFragment());
        fragmentList.add(new MineFragment());
        //将fragmentList传入适配器中
        adapter.setmList(fragmentList);

        pager.setAdapter(adapter);
        //设置viewPager滑动监听
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //拿到pager角标设置 bottomNavigationMenu所选中的菜单项
                onPagerSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //设置 bottomNavigationMenu监听，点击菜单项时 page页面实现联动
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
                switch (item.getItemId()){
                    case R.id.menu_home:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.menu_star_brand:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.menu_media_reports:
                        pager.setCurrentItem(2);
                        break;
                    case R.id.menu_attention:
                        pager.setCurrentItem(3);
                        break;
                    case R.id.menu_mine:
                        pager.setCurrentItem(4);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        //媒体报道处设置未读消息标志
        BadgeDrawable badge = navigation.getOrCreateBadge(R.id.menu_media_reports);
        badge.setNumber(66);
        badge.setMaxCharacterCount(3);
    }

    /**
     * 根据 viewPager传的角标设置 bottomNavigationMenu所选中的菜单项
     * @param position
     */
    private void onPagerSelected(int position) {
        switch (position){
            case 0:
                navigation.setSelectedItemId(R.id.menu_home);
                break;
            case 1:
                navigation.setSelectedItemId(R.id.menu_star_brand);
                break;
            case 2:
                navigation.setSelectedItemId(R.id.menu_media_reports);
                //移除媒体报道处消息标志
                navigation.removeBadge(R.id.menu_media_reports);
                break;
            case 3:
                navigation.setSelectedItemId(R.id.menu_attention);
                break;
            case 4:
                navigation.setSelectedItemId(R.id.menu_mine);
                break;
            default:
                break;
        }
    }
}